(function() {
    'use strict';

    const   //ws = require('ws'),
            util = require('util'),
           // io = require('socket.io-client'),
           // socket = require('socket.io-client')('ws://192.168.2.105/camera');
            events = require('events'),
            MJPEGStream = require('./image2muxer');

    class MJPEGVideoStream extends events.EventEmitter {

        constructor(options) {
            super();
            //this.name = options.name;
            this.streamUrl = options.streamUrl;
            this.gatewayid = options.gatewayid;
            this.streamid  = options.streamid;
            //this.width = options.width;
            //this.height = options.height;
            this.cb = options.cb;
            this.socket = options.socket;
            this.item = options.id;
            //this.startMJPEGStream();
        }

        stopStream() {
            this.MJPEGStream.closeMuxer();
            //this.wsServer.close();
        }
        startStream() {

            this.MJPEGStream= new MJPEGStream({
                url: this.streamUrl
            });

            this.pid = this.MJPEGStream.pid;
           
            this.MJPEGStream.on('image2data', (data) => {
                //console.log(".");
                this.cb(this.socket,this.item,data);
            });

            this.MJPEGStream.on('image2error', (data) => {
                return global.process.stderr.write(data);
            });
        }
       
    }

    module.exports = MJPEGVideoStream;

}());
